<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/8 0008
 * Time: 下午 2:59
 */
namespace app\admins\controller;

use think\Controller;
use think\Db;
use think\db\Query;
use think\Log;
use think\Request;

class Account extends Controller
{
    public function login()
    {
        return $this->fetch();
    }


    public function tt()
    {
        Query::event('before_select',function($options,$query){
            // 事件处理
            echo 'this is query event ';
        });
        $res = Db::table('admins')->where('username', 123)->select();
        var_dump($res);
        echo Db::table('admins')->getLastSql();
    }

    /**
     *
     */
    public function dologin()
    {

        $username = trim(input('post.username'));
        $pwd = trim(input('post.pwd'));
        $verifycode = trim(input('post.verifycode'));
        if ($username == "") {
            return json_encode(['code' => '1', 'msg' => '用户名不能为空']);
        }
        if ($pwd == "") {
            return json_encode(['code' => '1', 'msg' => '密码不能为空']);
        }
        if ($verifycode == "") {
            return json_encode(['code' => '1', 'msg' => '验证码不能为空']);
        }
        if (!captcha_check($verifycode)) {
            return json_encode(['code' => '1', 'msg' => '验证码不正确']);
        }
        $res = Db::table('admins')->where(['username' => $username])->find();
        if ($res) {
            if (md5($res['username'].$pwd)!=$res['password']) {
                logs('后台密码='.md5($res['username'].$pwd),'xyhua');
                return json_encode(['code' => '1', 'msg' => '密码错误']);
            }
            if ($res['status'] == 1) {
                return json_encode(['code'=>'1','msg'=>'用户被禁用']);
            }
        } else {
            return json_encode(['code' => '1', 'msg' => '用户名不存在']);
        }

        //判断用户状态
        session('admin', $res);
       return json_encode(['code' => '0', 'msg' => '登陆成功']);
    }


}